package com.atguigu1.core.operator

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 *
 * @description: combineByKey案例
 * @time: 2021-03-12 11:45
 * @author: baojinlong
 **/
object Spark25ReduceAggregateFoldCombine {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("rdd")
    // 设置rdd分区数字
    val sparkContext = new SparkContext(conf)
    // 排序后分区数量不变 hash range分区 python分区是私有的
    val rddValue: RDD[(String, Int)] = sparkContext.makeRDD(Seq(("a", 1), ("a", 2), ("b", 3), ("b", 4), ("b", 5), ("a", 6)), 2)
    rddValue.reduceByKey(_ + _)
    rddValue.aggregateByKey(0)(_ + _, _ + _)
    rddValue.foldByKey(0)(_ + _)
    rddValue.combineByKey(v => v, (x: Int, y: Int) => x + y, (x: Int, y: Int) => x + y)
    // 上面四个算子都能实现wordCount案例
    sparkContext.stop()
  }
}
